package com.iams.manage.controller;

import java.util.List;
import java.util.Map;

import com.iams.manage.service.IElasticsearchArchiveService;
import com.iams.manage.domain.Document;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.iams.common.annotation.Log;
import com.iams.common.core.controller.BaseController;
import com.iams.common.core.domain.AjaxResult;
import com.iams.common.enums.BusinessType;
import com.iams.manage.domain.Archive;
import com.iams.manage.service.IArchiveService;
import com.iams.common.utils.poi.ExcelUtil;
import com.iams.common.core.page.TableDataInfo;

import com.iams.elasticsearch.domain.ElasticsearchArchive;

/**
 * 档案列表Controller
 * 
 * @author zhjm
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/manage/archive")
public class ArchiveController extends BaseController
{
    @Autowired
    private IArchiveService archiveService;

    @Autowired
    private IElasticsearchArchiveService ElasticsearchArchiveService;

    /**
     * 查询档案列表列表
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:list')")
    @GetMapping("/list")
    public TableDataInfo list(Archive archive)
    {
        startPage();
        List<Archive> list = archiveService.selectArchiveList(archive);
        return getDataTable(list);
    }


    /**
     * 全文检索：内容
     */
    @GetMapping("/Elasticsearch")
    public TableDataInfo contentList(@RequestParam Map<String, String> allParams)
    {
        startPage();
        List<ElasticsearchArchive> list = ElasticsearchArchiveService.CombineSearch(allParams);
        return getDataTable(list);
    }


    /**
     * 导出档案列表列表
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:export')")
    @Log(title = "档案列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Archive archive)
    {
        List<Archive> list = archiveService.selectArchiveList(archive);
        ExcelUtil<Archive> util = new ExcelUtil<Archive>(Archive.class);
        util.exportExcel(response, list, "档案列表数据");
    }

    /**
     * 获取档案列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(archiveService.selectArchiveById(id));
    }

    /**
     * 获取档案权限
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:query')")
    @GetMapping(value = "/permission/{id}")
    public AjaxResult getPermissionInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(archiveService.checkArchivePermission(id));
    }



    /**
     * 新增档案列表
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:add')")
    @Log(title = "档案列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Archive archive)
    {
        return toAjax(archiveService.insertArchive(archive));
    }

    /**
     * 修改档案列表
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:edit')")
    @Log(title = "档案列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Archive archive)
    {
        return toAjax(archiveService.updateArchive(archive));
    }

    /**
     * 删除档案列表
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:remove')")
    @Log(title = "档案列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(archiveService.deleteArchiveByIds(ids));
    }

    /**
     * 新增档案文件列表
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:edit')")
    @Log(title = "档案列表", businessType = BusinessType.UPDATE)
    @PutMapping("/directory")
    public AjaxResult addDirectoryFiles(@RequestBody Document document)
    {
        return toAjax(archiveService.addDirectoryFiles(document));
    }

    /**
     * 删除档案文件列表
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:edit')")
    @Log(title = "档案列表", businessType = BusinessType.UPDATE)
    @PutMapping("/directory/delete")
    public AjaxResult deleteDirectoryFiles(@RequestBody Document document) {
        return toAjax(archiveService.deleteDirectoryFiles(document));
    }

    /**
     * 根据location查询档案列表
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:list')")
    @GetMapping("/location")
    public TableDataInfo getArchiveByLocation(@RequestParam String location) {
        List<Archive> list = archiveService.getArchiveByLocation(location);
//        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 拖拽移动档案
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:move')")
    @Log(title = "档案列表", businessType = BusinessType.UPDATE)
    @PostMapping("/move")
    public AjaxResult moveArchive(@RequestBody Map<String, Object> requestParams) {
        Long archiveId = Long.parseLong(requestParams.get("archiveId").toString());
        String targetLocation = requestParams.get("targetLocation").toString();
        return toAjax(archiveService.moveArchive(archiveId, targetLocation));
    }

    /**
     * 档案全部下架
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:update')")
    @Log(title = "档案列表", businessType = BusinessType.UPDATE)
    @PostMapping("/updateLocation")
    public AjaxResult takeOff(@RequestBody List<Archive> archives) {
        return toAjax(archiveService.takeOffArchives(archives));
    }

    /**
     * 获取未上架的档案列表
     */
    @PreAuthorize("@ss.hasPermi('manage:archive:list')")
    @GetMapping("/upArchiveList")
    public TableDataInfo getUpArchiveList(@RequestParam Map<String, Object> query) {
        startPage();
        List<Archive> list = archiveService.getUpArchiveList(query);
        return getDataTable(list);
    }
}
